#  Copyright (c) 1997-2009
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
#  http://www.polymake.de
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#-----------------------------------------------------------------------------

###########################################################
#
#  Building and installation
#
.PHONY: all all-java all-perlx perlx install install-bin install-shared install-java install-native-java \
	install-perlx configure reconfigure clean clean-bin clean-java distclean TAGS RTAGS test

ProjectTop := ${CURDIR}
_allow_lacking_BuildDir := y
include support/utils.make

_Configure = ${PERL} support/configure.pl $1 ${ConfigParams}

ifndef BuildDir

PerlxDir := .bootstrap/perlx-${PerlVersion}-${PerlArch}
INSTALL_PL := ${ProjectTop}/support/install.pl

all install% config-% : perlx
	@$(call _Configure, -quick -bootstrap)
	@rm -rf .bootstrap
	@$(MAKE) --no-print-directory $@

configure : perlx
	@$(call _Configure, -bootstrap)
	@rm -rf .bootstrap

perlx : all-perlx
	@$(MAKE) -C ${PerlxDir} pure_install InstallDir=${CURDIR}/.bootstrap

else  # BuildDir exists

### load configuration
include ${BuildDir}/conf.make

PerlxDir := ${BuildDir}/perlx-${PerlVersion}-${PerlArch}

Apps := $(notdir $(filter-out $(call _get_no_export,apps/*,broken|test), $(wildcard apps/*)))

define _MakeAppBin
	$(MAKE) -C ${BuildDir}/apps/$(1) AppName=$(1) $(2)

endef
define _MakeAppsBin
	$(foreach a,${Apps},$(call _MakeAppBin,$a,BeFirst=$(filter $a,$(firstword ${Apps})) $(1)))
endef
define _InstallSubdir
${PERL} ${INSTALL_PL} -m ${DirMask} -U $(2) $(1) ${InstallTop}/$(1)

endef
define _CreateDir
${PERL} ${INSTALL_PL} -d -m ${DirMask} $(1)

endef

define _MakeAppJava
	$(if $(wildcard java/src/${PackageBase}/$(1)),$(MAKE) -j1 -C java_build/${PackageBase}/$(1) $(2),:)

endef
_MakeAppsJava =	$(foreach a,${Apps} console,$(call _MakeAppJava,$a,BeFirst=$(filter $a,$(firstword ${Apps})) $(1)))

all : $(if ${.svn},perlx,all-perlx)
	@+$(call _MakeAppsBin,all-lib)
	@+$(call _MakeAppsJava,all)

all-java clean-java install-java install-native-java:
	@+$(call _MakeAppsJava,$(patsubst %-java,%,$@))

reconfigure : perlx
	@$(call _Configure, -repeat)

### installation

# scripts in support/ which should not be copied to the final installation location
InstHelpers := configure.pl convert_main_script locate_build_dir find-provides

define _InstallShared
	$(foreach d, perllib rules scripts, $(if $(wildcard $(1)/$d), $(call _InstallSubdir,$(1)/$d)))
endef

install : install-shared install-bin

install-bin : all install-perlx
	@+$(call _MakeAppsBin,install)
	@+$(call _MakeAppsJava,install-native)
	[ -d ${InstallBin} ] || $(call _CreateDir,${InstallBin})
	${PERL} support/convert_main_script --bindir ${InstallBin} \
		$(if ${PREFIX},--prefix ${PREFIX}) $(if ${FinkBase},--perllib ${FinkBase}/lib/perl5) \
		InstallTop=${FinalInstallTop} InstallArch=${FinalInstallArch} Arch="${Arch}"
	rm -f ${InstallArch}/conf.make
	{ sed -e '/Install.*=/ s/^/override /' \
	      -e '/INSTALL_PL=/ s:=.*:=$${InstallTop}/support/install.pl:' \
	      -e '/DESTDIR=/ { s/^/override /; q; }' \
	      -e '/^PerlExe/,/^PackageBase/d' \
	      ${BuildDir}/conf.make; \
	} >${InstallArch}/conf.make
	chmod 444 ${InstallArch}/conf.make

install-perlx : all-perlx
	$(call _CreateDir,${InstallArch})
ifndef RPM_BUILD_ROOT
	@$(MAKE) -C ${PerlxDir} pure_install InstallDir=${FinalInstallArch}
endif

install-shared:
	$(call _CreateDir,${InstallTop})
	$(call _InstallSubdir,perllib)
	$(foreach a, ${Apps}, $(call _InstallShared,apps/$a))
	@+$(call _MakeAppsBin, install-src)
	$(if ${.svn},${PERL} perl/polymake --script none::filter_rules -m ${InstallTop} ${Apps})
	$(call _MakeAppsJava,install)
	$(call _InstallSubdir,povray)
	$(call _InstallSubdir,scripts)
	$(call _InstallSubdir,xml)
	$(call _InstallSubdir,support,$(foreach f,${InstHelpers},-X $f))

### manual re-configuration
configure : perlx
	@$(call _Configure)

config-% :
	@echo $($*)

perlx : all-perlx
	@$(MAKE) -C ${PerlxDir} pure_install InstallDir=${CURDIR}/${BuildDir}

ifdef .svn

### unit tests

test : all
	${PERL} perl/polymake --script none::run_testcases --applications ${Apps}

### maintenance

tagsFLAGS = -R -e -f $@ --exclude=.svn --exclude='.\#*' --exclude='\#*' --exclude='*~'

TAGS:
	ctags ${tagsFLAGS} --language-force=c++ lib apps/*/{src,include}
RTAGS:
	ctags ${tagsFLAGS} --language-force=perl --exclude=testsuite --exclude=ext \
			   perl perllib apps/*/{perllib,rules,scripts} scripts support/*.pl apps/*/testsuite/*/test*.pl
	ctags -e -f $@ --append --language-force=c perl/ext/*.{xs,h,c}

include support/export.make

endif  # .svn

endif  # BuildDir exists

all-perlx:
	[ -d ${PerlxDir} ] || ${PERL} ${INSTALL_PL} -d -m 755 ${PerlxDir}
	cd ${PerlxDir}; TOP=${CURDIR} ${PERL} ${CURDIR}/perl/ext/Makefile.PL
	@$(MAKE) -C ${PerlxDir} all

### cleanup

clean-bin:
ifdef BuildDir
	@+$(call _MakeAppsBin, clean)
	rm -rf ${PerlxDir} ${BuildDir}/perlx/${PerlVersion}/${PerlArch}
endif

clean : clean-bin
	@+$(call _MakeAppsJava, clean)

distclean : clean
	rm -rf build* .bootstrap


# Local Variables:
# mode: Makefile
# End:
